import cv2
import os


def get_video_conf(source_video):
    cap = cv2.VideoCapture(source_video)
    if not cap.isOpened():
        print('video opened failed !')
    else:
        f_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        f_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
        fps = round(cap.get(cv2.CAP_PROP_FPS))
        frame_number = cap.get(cv2.CAP_PROP_FRAME_COUNT)
        duration = frame_number / fps
        print("FPS : ", fps)
        print("f_width : ", f_width, "f_height: ", f_height)
        print("all frame number : ", frame_number)
        print("duration : ", duration)
    cap.release()


def get_video_all_frame(video_path):
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        print('video opened failed !')
    else:
        frame_num = 0
        while True:
            _, im = cap.read()
            if im is None:
                break
            frame_num += 1
            print('Frame ID:', frame_num)
        cap.release()


if __name__ == "__main__":
    root_path = r'/home/stark/algo-env/datasets/dataset-guarantee/sample-origin'
    # video_name = os.listdir(root_path)
    video_name = ["0224-A-2023-02-15_09-23-07(2).mp4"]
    for tmp_source in video_name:
        source_file = os.path.join(root_path, tmp_source)
        print("source file:", source_file)
        get_video_conf(source_file)
    # video_path_01 = '/home/stark/algo-env/yolov5-tracker/data/abnormal_04_09_2.mp4'
    # get_video_all_frame(video_path_01)
